package dolf.window;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import dolf.game.BloodManager;
import dolf.objects.ImageStore;

public class PreferencesWindow extends Window {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static PreferencesWindow instance = new PreferencesWindow();
	private int currTab = 0;
	
	public PreferencesWindow() {
		initialize(SystemMessagesHandler.getInstance().getMessages().getString("preferences"));
	}
	
	public static PreferencesWindow getInstance() {
		return instance;
	}
	
	public void draw() {
		getContentPane().removeAll();
		initialize(SystemMessagesHandler.getInstance().getMessages().getString("preferences"));
		
		final JTabbedPane tabPane = new JTabbedPane();
		tabPane.setBorder(new EmptyBorder(0,0,10,0));
		
		JPanel generalPane = new JPanel();
		generalPane.setLayout(new GridBagLayout());										
		
		JPanel gamePane = new JPanel();
		gamePane.setLayout(new GridBagLayout());
		
		JPanel graphicsPane = new JPanel();
		graphicsPane.setLayout(new GridBagLayout());
		
		JPanel soundPane = new JPanel();
		soundPane.setLayout(new GridBagLayout());
		
		int zeile = 0;
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		
		//generalPanel
		
		
		java.util.ArrayList<String> langStrings = new java.util.ArrayList<String>();
		//search in lang.index for language-files
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
			this.getClass().getClassLoader().getResourceAsStream("dolf/lang/lang.index")));
			String lang = null;
			while ((lang = in.readLine()) != null) {
				//langStrings.add(new java.util.Locale(lang).getDisplayName());
				langStrings.add(lang);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		Object[] langArray = langStrings.toArray();
		java.util.Arrays.sort(langArray);
		final JComboBox langBox = new JComboBox( langArray );
		langBox.setSelectedItem( 
				ConfigurationHandler.getInstance().getProperty("lang"));
				//new java.util.Locale( ConfigurationHolder.getInstance()..getProperty("lang", java.util.Locale.getDefault().getLanguage()) ).getDisplayName());
		
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		generalPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("language")+": "), constraints);
		
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		generalPane.add(langBox, constraints);
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,50,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		generalPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("save_dir")+":"), constraints);
		
		constraints.gridx = 3;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		final JTextField pathBox = new JTextField();
		pathBox.setEditable(false);
		pathBox.setText(ConfigurationHandler.getInstance().getProperty("path_save"));
		generalPane.add(pathBox, constraints);
		
		constraints.gridx = 4;
		constraints.gridy = zeile++;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		JButton dirButton = new JButton();
		dirButton.setBorderPainted(false);
		dirButton.setContentAreaFilled(false);
		dirButton.setFocusPainted(false);
		dirButton.setIcon(ImageStore.getInstance().getIcon("dolf/gfx/window/directory.png"));
		dirButton.setMargin(new Insets(0,0,0,0));
		dirButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser(pathBox.getText());
			    fileChooser.setDialogTitle(SystemMessagesHandler.getInstance().getMessages().getString("save_dir"));
			    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			    fileChooser.setAcceptAllFileFilterUsed(false);
			    fileChooser.setFileHidingEnabled(false);
			    
			    if (fileChooser.showOpenDialog(instance) == JFileChooser.APPROVE_OPTION) { 
			    	pathBox.setText(fileChooser.getSelectedFile().getAbsolutePath());
			    }
			}
		});
		generalPane.add(dirButton, constraints);
		
		
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		generalPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("logging")+":"), constraints);
		
		
		final JCheckBox logBox = new JCheckBox();
		logBox.setSelected(ConfigurationHandler.getInstance().getProperty("log_enabled").compareTo("true") == 0 ? true : false);
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		generalPane.add(logBox, constraints);
		
		generalPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("log_dir")+":"), constraints);
		
		
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,50,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		generalPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("log_dir")+":"), constraints);
		
		constraints.gridx = 3;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		final JTextField logTextBox = new JTextField();
		logTextBox.setEditable(false);
		logTextBox.setText(ConfigurationHandler.getInstance().getProperty("log_dir"));
		generalPane.add(logTextBox, constraints);

		if(!logBox.isSelected()) {
			logTextBox.setEnabled(false);
		}
		logBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if(logBox.isSelected()) {
					logTextBox.setEnabled(true);
				}
				else logTextBox.setEnabled(false);
			}
		});
		
		constraints.gridx = 4;
		constraints.gridy = zeile++;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		JButton dirButton2 = new JButton();
		dirButton2.setBorderPainted(false);
		dirButton2.setContentAreaFilled(false);
		dirButton2.setFocusPainted(false);
		dirButton2.setMargin(new Insets(0,0,0,0));
		dirButton2.setIcon(ImageStore.getInstance().getIcon("dolf/gfx/window/directory.png"));
		dirButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!logTextBox.isEnabled()) return;
				
				JFileChooser fileChooser = new JFileChooser(logTextBox.getText());
			    fileChooser.setDialogTitle("Select "+SystemMessagesHandler.getInstance().getMessages().getString("log_dir"));
			    fileChooser.setFileHidingEnabled(false);
			    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			    fileChooser.setAcceptAllFileFilterUsed(false);
			    
			    
			    if (fileChooser.showOpenDialog(instance) == JFileChooser.APPROVE_OPTION) { 
			    	logTextBox.setText(fileChooser.getSelectedFile().getAbsolutePath());
			    }
			}
		});
		generalPane.add(dirButton2, constraints);
		
		//ende generalPanel
		
		
		//gamePanel
		final JLabel showPlName = new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("show_player_name"));
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		gamePane.add(showPlName, constraints);
		
		final JCheckBox plNameBox = new JCheckBox();
		plNameBox.setSelected(ConfigurationHandler.getInstance().getProperty("show_playername").compareTo("true") == 0 ? true : false);
		
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		gamePane.add(plNameBox, constraints);
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5, 50, 5,5);
		constraints.anchor = GridBagConstraints.WEST;
		gamePane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("start_money")+":"), constraints);
		
		
		final JSlider moneySlider = new JSlider(0, 1000);
		moneySlider.setValue(Integer.parseInt(ConfigurationHandler.getInstance().getProperty("money")));
		moneySlider.setMinorTickSpacing(100);
		moneySlider.setMajorTickSpacing(250);
		moneySlider.setPaintTicks(true); 
		// Bei 50 und 100 einen Text setzen
		moneySlider.setPaintLabels(true);
		
		final JLabel selMoney = new JLabel(Integer.toString(moneySlider.getValue()));
		selMoney.setPreferredSize(new Dimension(40,10));
		
		moneySlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				selMoney.setText(Integer.toString(moneySlider.getValue()));
			}	
		});

		constraints.gridx = 3;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		gamePane.add(moneySlider, constraints);
		
		constraints.gridx = 4;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		gamePane.add(selMoney, constraints);			
		
		//neue zeile
		final JLabel weaponLabel = new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("weapons"));
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		gamePane.add(weaponLabel, constraints);
		
		final JCheckBox weaponBox = new JCheckBox();
		weaponBox.setSelected(ConfigurationHandler.getInstance().getProperty("weapons").compareTo("true") == 0 ? true : false);
		
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		gamePane.add(weaponBox, constraints);
		
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5, 50, 5,5);
		constraints.anchor = GridBagConstraints.WEST;
		gamePane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("health")+":"), constraints);
		
		final JSlider hpSlider = new JSlider(1, 300);
		hpSlider.setValue(Integer.parseInt(ConfigurationHandler.getInstance().getProperty("health")));
		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put( new Integer( 1 ), new JLabel("1") );
		labelTable.put( new Integer( 100 ), new JLabel("100") );
		labelTable.put( new Integer( 200 ), new JLabel("200") );
		labelTable.put( new Integer( 300 ), new JLabel("300") );
		hpSlider.setLabelTable( labelTable );

		//hpSlider.setMinorTickSpacing(0);
		hpSlider.setMajorTickSpacing(99);
		hpSlider.setPaintTicks(true); 
		hpSlider.setPaintLabels(true);

		final JLabel selHP = new JLabel(Integer.toString(hpSlider.getValue()));
		selHP.setPreferredSize(new Dimension(40,10));
		
		hpSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				selHP.setText(Integer.toString(hpSlider.getValue()));
			}	
		});
		
		constraints.gridx = 3;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		gamePane.add(hpSlider, constraints);
		
		constraints.gridx = 4;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		gamePane.add(selHP, constraints);
	
		//gamePanel end
		
		
		// graphicsPanel
		zeile = 0;
		
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("antialiasing")+":"), constraints);
		
		
		final JCheckBox aaBox = new JCheckBox();
		aaBox.setSelected( ConfigurationHandler.getInstance().getProperty("antialiasing").compareTo("true") == 0 ? true : false );
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		graphicsPane.add(aaBox, constraints);
		
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,50,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("interpolation")+": "), constraints);
		
		String[] intStrings = {SystemMessagesHandler.getInstance().getMessages().getString("nearest"),SystemMessagesHandler.getInstance().getMessages().getString("bilinear"),SystemMessagesHandler.getInstance().getMessages().getString("bicubic")};
		final JComboBox intBox = new JComboBox(intStrings);
		int intVal = Integer.parseInt(ConfigurationHandler.getInstance().getProperty("interpolation"));
		if (intVal < 0 || intVal > 2) {
			System.err.println("ERROR: invalid interpolation-value");
			intVal = 0;
		}
		intBox.setSelectedIndex(intVal);
		constraints.gridx = 3;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		graphicsPane.add(intBox, constraints);
		
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		graphicsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("dithering")+":"), constraints);
		
		final JCheckBox dithBox = new JCheckBox();
		dithBox.setSelected(ConfigurationHandler.getInstance().getProperty("dithering").compareTo("true") == 0 ? true : false);
		constraints.gridx = 1;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(dithBox, constraints);
		
		
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("blood")+":"), constraints);
		
		final JCheckBox bloodBox = new JCheckBox();
		final JCheckBox keepBloodBox = new JCheckBox();
		final JSlider bloodSlider = new JSlider(0, 100);
		bloodSlider.setSnapToTicks(true);
		bloodBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if (!bloodBox.isSelected()) {
					bloodSlider.setEnabled(false);
					keepBloodBox.setEnabled(false);
				}
				else {
					bloodSlider.setEnabled(true);
					keepBloodBox.setEnabled(true);
				}
			}
			
		});
		
		bloodBox.setSelected(ConfigurationHandler.getInstance().getProperty("blood").compareTo("true") == 0 ? true : false);
		
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		graphicsPane.add(bloodBox, constraints);
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5, 50, 5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("bloodlevel")+":"), constraints);
		
		
		bloodSlider.setValue(Integer.parseInt(ConfigurationHandler.getInstance().getProperty("bloodlevel")));
		bloodSlider.setMinorTickSpacing(10);
		bloodSlider.setMajorTickSpacing(25);
		bloodSlider.setPaintTicks(true); 
		bloodSlider.setPaintLabels(true);
		
		final JLabel selBlood = new JLabel(Integer.toString(bloodSlider.getValue()));
		selBlood.setPreferredSize(new Dimension(40,10));
		
		bloodSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				selBlood.setText(Integer.toString(bloodSlider.getValue()));
			}	
		});
		
		if (!bloodBox.isSelected()) {
			bloodSlider.setEnabled(false);
		}
		
		constraints.gridx = 3;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(bloodSlider, constraints);
		
		constraints.gridx = 4;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		graphicsPane.add(selBlood, constraints);
		
		//neue zeile
		
		final JLabel keepBloodLabel = new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("keep_blood"));
		constraints.gridx = 0;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		graphicsPane.add(keepBloodLabel, constraints);
		
		if (!bloodBox.isSelected()) {
			keepBloodBox.setEnabled(false);
		}
		keepBloodBox.setSelected(ConfigurationHandler.getInstance().getProperty("keep_blood").compareTo("true") == 0 ? true : false);
		
		constraints.gridx = 1;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		graphicsPane.add(keepBloodBox, constraints);
		
		
	
		
		UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
		
		ArrayList<String> lafNames = new ArrayList<String>();
		
		for (int i = 0; i < lookAndFeelInfos.length; i++) {
			lafNames.add(lookAndFeelInfos[i].getName());            
		}
		Object[] lafArray = lafNames.toArray();
		java.util.Arrays.sort(lafArray);
		final JComboBox lafBox = new JComboBox( lafArray );
		
		//get selected laf
		UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
		for (UIManager.LookAndFeelInfo laf:lafs) {
			if (laf.getClassName().equals(ConfigurationHandler.getInstance().getProperty("lookandfeel")) ) {
				lafBox.setSelectedItem(laf.getName());
				break;
			}
		}
		
		
		lafBox.setSelectedItem( 
				ConfigurationHandler.getInstance().getProperty("lookandfeel"));
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.insets = new Insets(5, 50, 5,5);
		graphicsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("lookandfeel")+": "), constraints);
		
		constraints.gridx = 3;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		graphicsPane.add(lafBox, constraints);
		
		java.util.ArrayList<String> themeStrings = new java.util.ArrayList<String>();
		//search in theme.index for language-files
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
			this.getClass().getClassLoader().getResourceAsStream("dolf/window/themes/theme.index")));
			String theme = null;
			while ((theme = in.readLine()) != null) {
				themeStrings.add(theme);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		Object[] themeArray = themeStrings.toArray();
		java.util.Arrays.sort(themeArray);
		final JComboBox themeBox = new JComboBox( themeArray );
		themeBox.setSelectedItem(ConfigurationHandler.getInstance().getProperty("theme"));
		
		if (!lafBox.getSelectedItem().equals("Metal")) {
			themeBox.setEnabled(false);
		}
		else themeBox.setEnabled(true);
		
		lafBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if (!lafBox.getSelectedItem().equals("Metal")) {
					themeBox.setEnabled(false);
				}
				else themeBox.setEnabled(true);
			}
			
		});

		
		themeBox.setSelectedItem( 
				ConfigurationHandler.getInstance().getProperty("theme"));
		
		constraints.gridx = 2;
		constraints.gridy = zeile;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(5,50,5,5);
		constraints.anchor = GridBagConstraints.WEST;
		JLabel themeLabel = new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("theme")+": ");
		themeLabel.setToolTipText(SystemMessagesHandler.getInstance().getMessages().getString("only_available_when_metal_laf"));
		graphicsPane.add(themeLabel, constraints);
		
		constraints.gridx = 3;
		constraints.gridy = zeile++;
		constraints.insets = new Insets(5,5,5,5);
		constraints.anchor = GridBagConstraints.EAST;
		themeBox.setToolTipText(SystemMessagesHandler.getInstance().getMessages().getString("only_available_when_metal_laf"));
		graphicsPane.add(themeBox, constraints);
		
		
		
		//graphicsPanel end
		
		//soundPane start
		JLabel nosound = new JLabel("Sorry, sound is not yet available");
		soundPane.add(nosound);
		
		//soundPane end
		
		
		// buttons
		JPanel buttonPane = new JPanel();
		buttonPane.setVisible(true);
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		final JButton defaultButton = new JButton(SystemMessagesHandler.getInstance().getMessages().getString("default"));
		defaultButton.setMnemonic('D');
		
		final JButton cancelButton = new JButton(SystemMessagesHandler.getInstance().getMessages().getString("cancel"));
		cancelButton.setMnemonic('c');
		
		final JButton okButton = new JButton(SystemMessagesHandler.getInstance().getMessages().getString("ok"));
		okButton.setMnemonic('s');
		
		final JButton applyButton = new JButton(SystemMessagesHandler.getInstance().getMessages().getString("apply"));
		applyButton.setMnemonic('a');
		
		//actionListener

		defaultButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				langBox.setSelectedItem("en");
				moneySlider.setValue(200);
				hpSlider.setValue(100);
				bloodBox.setSelected(true);
				keepBloodBox.setSelected(true);
				plNameBox.setSelected(true);
				lafBox.setSelectedItem("Metal");
				themeBox.setSelectedItem("Ocean");
				pathBox.setText(System.getProperty("user.home")+java.io.File.separator+".dolf");
				logBox.setSelected(false);
				logTextBox.setText(System.getProperty("user.home")+java.io.File.separator+".dolf");
				logTextBox.setEnabled(false);
				bloodBox.setSelected(false);
				
				aaBox.setSelected(true);
				dithBox.setSelected(true);
				intBox.setSelectedIndex(2);
			} 
		});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				instance.dispose();
			} 
		});
		class SaveListener implements ActionListener {
			public void actionPerformed(ActionEvent e) { 
				//game
				ConfigurationHandler config = ConfigurationHandler.getInstance();
				config.setProperty("lang", langBox.getSelectedItem().toString());
				config.setProperty("money", Integer.toString(moneySlider.getValue()));
				config.setProperty("blood", bloodBox.isSelected() ? "true" : "false");
				config.setProperty("bloodlevel", Integer.toString(bloodSlider.getValue()));
				if (bloodBox.isSelected()) {
					BloodManager.getInstance().changeBloodyGame(bloodSlider.getValue()/100.0);
				} else BloodManager.getInstance().changeBloodyGame(0);
				config.setProperty("health", Integer.toString(hpSlider.getValue()));
				config.setProperty("keep_blood", keepBloodBox.isSelected() ? "true" : "false");
				config.setProperty("show_playername", plNameBox.isSelected() ? "true" : "false");
				config.setProperty("weapons", weaponBox.isSelected() ? "true" : "false");
				
//				get right path for selected LookAndFeel
				UIManager.LookAndFeelInfo[] lafs= UIManager.getInstalledLookAndFeels();
				for (UIManager.LookAndFeelInfo laf:lafs) {
					if (laf.getName().equals(lafBox.getSelectedItem())) {
						config.setProperty("lookandfeel", laf.getClassName());
						break;
					}
				}
				if (themeBox.getSelectedItem() != null) config.setProperty("theme", themeBox.getSelectedItem().toString());
				
				config.setProperty("path_save", pathBox.getText());
				config.setProperty("log_enabled", logBox.isSelected() ? "true" : "false");
				config.setProperty("log_dir", logTextBox.getText());
				
				
				//graphics
				config.setProperty("antialiasing", aaBox.isSelected() ? "true" : "false");
				config.setProperty("dithering", dithBox.isSelected() ? "true" : "false");
				config.setProperty("interpolation", Integer.toString(intBox.getSelectedIndex()));
				
				config.saveConfig();
			} 
		};
		class ApplyListener extends SaveListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				currTab = tabPane.getSelectedIndex();
				super.actionPerformed(e);
			}
		};
		class OkListener extends SaveListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				dispose();
				currTab = 0;
				super.actionPerformed(e);
			}
		};

		okButton.addActionListener(new OkListener());
		
		applyButton.addActionListener(new ApplyListener());

		buttonPane.add(defaultButton,java.awt.BorderLayout.LINE_START);
		buttonPane.add(Box.createRigidArea(new Dimension(10,0)));								
		
						
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(Box.createRigidArea(new Dimension(10,0)));
		buttonPane.add(cancelButton,java.awt.BorderLayout.CENTER);
		buttonPane.add(Box.createRigidArea(new Dimension(10,0)));
		buttonPane.add(applyButton,java.awt.BorderLayout.CENTER);
		buttonPane.add(Box.createRigidArea(new Dimension(10,0)));
		buttonPane.add(okButton,java.awt.BorderLayout.LINE_END);
		
				
		getRootPane().setDefaultButton(applyButton);
		
		JPanel genPane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
		genPane2.add(generalPane);
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("general"), genPane2);
		
		
		JPanel gamePane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
		gamePane2.add(gamePane);
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("game"), gamePane2);
		//to align top-left instead of centered
		JPanel graphicsPane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
		graphicsPane2.add(graphicsPane);
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("graphics"), graphicsPane2);
		
		JPanel soundPane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
		soundPane2.add(soundPane);
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("sound"), soundPane2);
		tabPane.setSelectedIndex(currTab);
		
		add(tabPane,BorderLayout.PAGE_START);
		add(new JSeparator());
		add(buttonPane, BorderLayout.PAGE_END);
		pack();
		setLocationRelativeTo(MainWindow.getInstance());
		setVisible(true);
	}
}
